package cn.itcast.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.itcast.dao.RoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;


    @Override
    @Transactional(readOnly = true)
    public PageResult findByPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(currentPage, pageSize);
        //根据条件分页查询检查项信息
        Page<Role> page = roleDao.selectByCondition(queryString);
        //构建分页数据返回
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Boolean save(List<Integer> permissionIds, List<Integer> menuIds, Role role) {
        Integer integer = roleDao.save(role);
        role = roleDao.findByName(role.getName());
        int count = 1;
        if (CollUtil.isNotEmpty(permissionIds)) {
            for (Integer permissionId : permissionIds) {
                integer += roleDao.saveRoleAndPermission(role.getId(), permissionId);
            }
            count += permissionIds.size();
        }
        if (CollUtil.isNotEmpty(menuIds)) {
            for (Integer menuId : menuIds) {
                integer += roleDao.saveRoleAndMenu(role.getId(), menuId);
            }
            count += menuIds.size();
        }
        return integer == count;
    }

    @Override
    public Boolean delete(Integer roleId) {
        Integer count = roleDao.findUsrIdsByRoleId(roleId);

        if (count>0){
            return false;
        }

        roleDao.deleteRoleAndMenu(roleId);
        roleDao.deleteRoleAndPermission(roleId);
        roleDao.deleteRoleAndUser(roleId);
        return roleDao.delete(roleId) > 0;
    }

    @Override
    public Boolean edit(List<Integer> permissionIds, List<Integer> menuIds, Role role) {
        roleDao.deleteRoleAndMenu(role.getId());
        roleDao.deleteRoleAndPermission(role.getId());

        if (CollUtil.isNotEmpty(permissionIds)) {
            for (Integer permissionId : permissionIds) {
                roleDao.saveRoleAndPermission(role.getId(), permissionId);
            }
        }
        if (CollUtil.isNotEmpty(menuIds)) {
            for (Integer menuId : menuIds) {
                roleDao.saveRoleAndMenu(role.getId(), menuId);
            }
        }
        return roleDao.edit(role) > 0;
    }

    @Override
    @Transactional(readOnly = true)
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Integer> findMenuIdsByRoleId(Integer roleId) {
        return roleDao.findMenuIdsByRoleId(roleId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Integer> findPermissionIdsByRoleId(Integer roleId) {
        return roleDao.findPermissionIdsByRoleId(roleId);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }


}
